#include "stdafx.h"
#include "GUIObject.h"

#include "ResourceManager.h"

namespace ZeEngine
{
    u32 GUIObject::m_currentId = 0;
    ResourceManager* GUIObject::m_pResourceManager = NULL;

    GUIObject::GUIObject() : m_hidden(false)
    {
        m_id = m_currentId++;
    }

    void GUIObject::Init(const Vector2D<float>& position, const Vector2D<float>& size)
    {
        m_position = position;
        m_size = size;
        m_rect.x = static_cast<int>(position.GetX());
		m_rect.y = static_cast<int>(position.GetY());
		m_rect.w = static_cast<int>(size.GetX());
		m_rect.h = static_cast<int>(size.GetY());
    }

    GUIObject::~GUIObject()
    {
        m_children.clear();
    }

    void GUIObject::SetPosition(const Vector2D<float> position) 
    { 
        Init(position, m_size);

        for (auto child : m_children)
        {
            if (child)
            {
                child->Init(position, child->m_size);
            }
        }
    }

    bool GUIObject::operator==(const GUIObject &other) const
    {
        return other.GetID() == GetID();
    }

    void GUIObject::AddChild(std::shared_ptr<GUIObject> pObject)
    {
        //Set the offset in the child to be the current positon of the parent
        pObject->m_offset = pObject->GetRelativePosition();

        //Add it to the current objects position so it will be offsetted
        pObject->Init(m_position, pObject->m_size);

        m_children.push_back(pObject);
    }
}
